<template>
  <view class="box" v-if="listData && listData.length">
    <exam-list-item v-for="item in listData" :key="item.taskId" :data="item" @click="link(item)"></exam-list-item>
    <exam-start-dialog v-model="dialogVisible" @confirm="startTest" :count-tip="countTip" :type="'primary'" />
  </view>
  <view v-else>
    <Empty></Empty>
  </view>
</template>

<script>
import { getTodoTestList, getStartTest } from '@/api/educational-training/online-examination'
import ExamListItem from './components/ExamListItem'
import ExamStartDialog from './components/ExamStartDialog'
import Empty from '@/components/cu-empty/index.vue'

export default {
  name: 'online-examination',
  props: {},
  components: {
    ExamListItem,
    ExamStartDialog,
    Empty
  },
  data() {
    return {
      listData: [],
      queryParams: {
        pageSize: 10,
        pageNum: 1,
        planType: 1,
        isFinish: false,
        userId: this.$store.state.user.staffId
      },
      total: 0,
      dialogVisible: false,
      countTip: '',
      currentExamId: ''
    }
  },
  created() {
    this.getList()
  },
  onShow() {
    this.getList()
  },
  methods: {
    async getList(isRefresh = true) {
      const { total, rows } = await getTodoTestList(this.queryParams).finally(() => uni.stopPullDownRefresh())
      if (isRefresh) {
        this.listData = rows || []
      } else {
        this.listData = [...this.listData, ...rows]
      }
      this.total = total
    },
    link(item) {
      this.currentExamId = item.taskId
      if (item.status === '2') {
        this.dialogVisible = true
        this.countTip = `${item.makeUpExamCount}`
      } else {
        uni.navigateTo({
          url: `/pages/work/online-examination/take-part-online-examination?examPlanId=${this.currentExamId}&start=true`
        })
      }
    },
    async startTest() {
      await getStartTest(this.currentExamId)
      uni.navigateTo({
        url: `/pages/work/online-examination/take-part-online-examination?examPlanId=${this.currentExamId}&start=true`
      })
    }
  },
  onNavigationBarButtonTap() {
    uni.navigateTo({
      url: '/pages/work/online-examination/examRecord'
    })
  },
  onPullDownRefresh() {
    this.queryParams.pageNum = 1
    this.getList()
  },
  onReachBottom() {
    const { pageNum, pageSize } = this.queryParams
    if (pageNum * pageSize < this.total) {
      this.queryParams.pageNum += 1
      this.getList(false)
    }
  }
}
</script>

<style scoped lang="scss">
.box {
  padding: 40rpx 0;
}
</style>
